home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / bank.arc / BANK.PAS next >
Pascal/Delphi Source File  |  1991-04-28  |  3KB  |  117 lines

  1. PROGRAM bankdemo;
  2. (* Turbo Pascal 5.5 program *)
  3. USES queues,crt;
  4. TYPE
  5.   PatronPt = ^PatronItem;
  6.   PatronItem = OBJECT (GenericItem)
  7.     TaskTime : Word;
  8.     CONSTRUCTOR Init(min,max : Byte);
  9.     FUNCTION GetTime : Word;
  10.     DESTRUCTOR Done; virtual;
  11.   END;
  12.  
  13.   ActionType = (NewPatron, Serving, Idle);
  14.  
  15.   Bank = Object
  16.     Q : queue;
  17.     time, timeleft, idletime : Word;
  18.     AvgMins, Shortest, Longest : Byte;
  19.     CONSTRUCTOR Init(AMBP, ST, LT : Byte);
  20.     DESTRUCTOR  Done;
  21.     FUNCTION    GetTime : Word;
  22.     PROCEDURE   Tick;
  23.   END;
  24.  
  25.   CONSTRUCTOR PatronItem.Init(min,max : Byte);
  26.   BEGIN
  27.     TaskTime := 10*(min+random(succ(max-min)));
  28.   END;
  29.  
  30.   DESTRUCTOR PatronItem.Done;
  31.   BEGIN  END;
  32.  
  33.   FUNCTION PatronItem.GetTime : Word;
  34.   BEGIN  GetTime := TaskTime;  END;
  35.  
  36.   CONSTRUCTOR Bank.Init(AMBP, ST, LT : Byte);
  37.   BEGIN
  38.     Randomize;
  39.     Q.Init;
  40.     time := 0; timeLeft := 0; idletime := 0;
  41.     AvgMins := AMBP;
  42.     Shortest := ST;
  43.     Longest := LT;
  44.     Write('Now beginning bank simulation.  ');
  45.     WriteLn('New patrons arrive on the average of');
  46.     Write('every ',AvgMins,' minutes.  Their business ');
  47.     WriteLn('takes from ',shortest,' to ',longest,' minutes.');
  48.   END;
  49.  
  50.   DESTRUCTOR Bank.Done;
  51.   BEGIN
  52.     WriteLn;
  53.     Write('The simulation ran for ',time DIV 10,'.',time MOD 10);
  54.     WriteLn(' simulated minutes.  Of that time, the clerk');
  55.     Write('was idle for ',idletime DIV 10,'.',idletime MOD 10);
  56.     Write(' simulated minutes.  Maximum length of ');
  57.     WriteLn('queue was ',Q.GetMax);
  58.     Q.done;
  59.   END;
  60.  
  61.   FUNCTION Bank.GetTime : Word;
  62.   BEGIN  GetTime := Time;  END;
  63.  
  64.   PROCEDURE Bank.Tick;
  65.   VAR
  66.     A : PatronPt;
  67.     G : ItemPtr;
  68.  
  69.     PROCEDURE Event(Act : ActionType; T, L : Word);
  70.     BEGIN
  71.       Write(T DIV 10:4,'.',T MOD 10,' minutes   ');
  72.       CASE Act OF
  73.         NewPatron : Write('New patron joined queue.  ');
  74.         Serving   : Write('Clerk is serving a patron.');
  75.         Idle      : Write('Clerk is idle.            ');
  76.       END;
  77.       WriteLn('  ',L:4,' in queue.');
  78.     END;
  79.  
  80.   BEGIN
  81.     Write(time DIV 10:4,'.',time MOD 10,^M);
  82.     IF random(10*AvgMins) = 0 THEN
  83.       BEGIN
  84.         Q.enqueue(new(PatronPt,Init(Shortest, Longest)));
  85.         Event(NewPatron, time, Q.length);
  86.       END;
  87.     IF timeleft <= 1 THEN
  88.       BEGIN
  89.         A := PatronPt(Q.dequeue);
  90.         IF A <> NIL THEN
  91.           BEGIN
  92.             timeleft := A^.GetTime;
  93.             Dispose(A,done);
  94.             Event(Serving, time, Q.length);
  95.           END
  96.         ELSE
  97.           BEGIN
  98.             Inc(IdleTime);
  99.             IF timeleft = 1 THEN Event(idle, time, Q.length);
  100.           END;
  101.       END;
  102.     Inc(Time);
  103.     IF timeleft > 0 THEN dec(timeleft);
  104.     delay(100);
  105.   END;
  106.  
  107.  
  108. VAR FirstNational : Bank;
  109.  
  110. BEGIN
  111.   Randomize;
  112.   FirstNational.Init(4,3,5);
  113.   REPEAT
  114.     FirstNational.tick;
  115.   UNTIL KeyPressed OR (FirstNational.GetTime >= 600);
  116.   FirstNational.done;
  117. END.